home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10312 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  48 lines

  1. Path: news.biddeford.com!usenet
  2. From: Tim Steiger <tsteiger@biddeford.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Kind of an annoying question...
  5. Date: Sat, 16 Mar 1996 13:56:55 -0800
  6. Organization: Biddeford Internet Corp.
  7. Message-ID: <314B3927.271A@biddeford.com>
  8. References: <4hvrio$c4k@lastactionhero.rs.itd.umich.edu>
  9. NNTP-Posting-Host: bidd23.biddeford.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Steve Palmer Peng wrote:
  16. > I'm wondering, just what function in C allows me to enter a character
  17. > from the keyboard, and it automatically executes the command I designate
  18. > that character to.  For instance, I have something like,
  19. > printf("Enter Y or N:");
  20. > and I want the command after the printf statement to wait for input
  21. > from the keyboard, but once someone enters a Y or an N, the next line of
  22. > code is executed, without hitting enter.  Does anyone know how?  It would
  23. > be greatly appreciated if someone could help me out here...
  24. > SteveHow about?
  25.  
  26. #include(conio.h)
  27. main()
  28. {
  29.   printf("Enter Y or N: ");
  30.   switch(getch())
  31.     {
  32.       case 'n':
  33.       case 'N': printf("Pressed N!\n");
  34.                 break;
  35.       case 'y':
  36.       case 'Y': printf("Pressed Y!\n");
  37.                 break;
  38.       default:  printf("Didn't press N or Y\n");
  39.     }
  40. }
  41.  
  42. ps: If you are in a Unix environment read up on the curses library.
  43.     % man curses
  44.